package com.example.testdbsearchform;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class ProductDAO {

	  private SQLiteDatabase database;
	  public ProductDAO(SQLiteDatabase database) {
		  this.database = database;
	  }

	  public Product createProduct(String name, Date date, Category category) {
	    ContentValues values = new ContentValues();
	    values.put("name", name);
	    values.put("date", date.getTime());
	    values.put("category", category.getId());
	    long insertId = database.insert("Product", null, values);
	    
	    Cursor cursor = database.
	    		  rawQuery("select * from Product where _id = ? ", 
	    				  new String[] { Long.toString(insertId) });
	    
	    if (!cursor.moveToFirst()) {
	    	return null;
	    }
	    Product newProduct = cursorToProduct(cursor);
	    cursor.close();
	    return newProduct;
	  }

	  public void deleteProduct(Product product) {
	    long id = product.getId();
	    System.out.println("Product deleted with id: " + id);
	    database.delete("Product", "_id = " + id, null);
	  }
	  
	  private String buildSQl(boolean where) {
		  String sql = "";
		  if (!where) {
    		sql = " where ";
    	} else {
    		sql = " and ";
    	}
		return sql;  
	  }

	  public List<Product> getAllProducts(String name, Long category, Date date) {
	    List<Product> products = new ArrayList<Product>();

	    boolean where = false;
	    String sql = "select * from Product ";
	    
	    if (name != null) {
	    	sql = sql + buildSQl(where);
	    	sql = sql + " name like '" +name+ "%' ";
	    	where = true;
	    }
	    if (category != null) {
	    	sql = sql + buildSQl(where);
	    	sql = sql + " category = '" +category+ "' ";
	    	where = true;
	    }
	    if (date != null) {
	    	sql = sql + buildSQl(where);
	    	sql = sql + " date = " +date.getTime()+ " ";
	    	where = true;
	    }
	    
	    Toast.makeText(CustomApplication.getInstance(), sql, Toast.LENGTH_LONG).show();
	    
	    Cursor cursor = database.
	    		  rawQuery(sql, new String[] {});
	    
	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	    	Product product = cursorToProduct(cursor);
	    	products.add(product);
	      cursor.moveToNext();
	    }
	    // Make sure to close the cursor
	    cursor.close();
	    return Collections.unmodifiableList(products);
	  }

	  private Product cursorToProduct(Cursor cursor) {
		Product product = new Product();
		product.setId(cursor.getLong(0));
		product.setDate(new Date(cursor.getLong(1)));
		product.setName(cursor.getString(2));
		
		Category category = CustomApplication.getInstance().getCategory(cursor.getLong(3));
		product.setCategory(category);
	    return product;
	  }
}
